import { formatTimeDate } from '../../../utils/formatData';
import { LuckyBeansHttp } from '../../../api/luckyBeans'

const luckyBeansModel = new LuckyBeansHttp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    luckyBeansInfo: {}, // 幸运豆信息
    billList: [], // 幸运豆账单列表
    page: 1,
    pageSize: 10,
    totalNum: 0,
    dateText: formatTimeDate(new Date(), 'yyyy-MM'),
    dataTimeType: 'year-month',
    currentDate: new Date().getTime(),
    currentSelectDate: new Date().getTime(),
    selectTimeRadio: 'month',
    isShowSelectTime: false,
    formatter(type, value) {
      switch (type) {
        case 'year':
          return `${value}年`;
        case 'month':
          return `${value}月`;
        case 'day':
          return `${value}日`;
        default:
          return value;
      }
    }
  },

  initPage () {
    this.httpGetLuckyBeansBill()
  },

  // 打开时间选择
  onOpenSelectTime () {
    this.setData({
      isShowSelectTime: true
    })
  },

  // 时间选择 - 取消
  onCancelTime () {
    this.setData({
      isShowSelectTime: false,
      currentDate: this.data.currentSelectDate
    })
  },

  // 时间选择 - 确定
  onConfirmTime () {
    const { selectTimeRadio, currentDate } = this.data
    let dateText = ''
    if (selectTimeRadio === 'month') {
      dateText = formatTimeDate(new Date(currentDate), 'yyyy-MM')
    } else if (selectTimeRadio === 'day') {
      dateText = formatTimeDate(new Date(currentDate), 'yyyy-MM-dd')
    }
    this.setData({
      isShowSelectTime: false,
      currentSelectDate: currentDate,
      dateText: dateText,
      page: 1
    })
    this.httpGetLuckyBeansBill()
  },

  // 修改时间类型选择单选框
  onChangeTimeRadio(event) {
    let dateType = ''
    let radioType = event.detail
    if (radioType === 'month') {
      dateType = 'year-month'
    } else if (radioType === 'day') {
      dateType = 'date'
    }
    this.setData({
      selectTimeRadio: radioType,
      dataTimeType: dateType
    });
  },

  // 时间选择器 - 当值变化时触发的事假
  onInputPicker (event) {
    this.setData({
      currentDate: event.detail
    });
  },

  // 页面相关事件处理函数--监听用户下拉动作
  onPullDownRefresh: function () {
    this.setData({
      page: 1
    })
    this.httpGetLuckyBeansBill()
  },

  // 页面上拉触底事件的处理函数
  onReachBottom: function () {
    this.setData({
      page: this.data.page + 1
    })
    this.httpGetLuckyBeansBill()
  },

  // 获取幸运豆明细
  httpGetLuckyBeansBill () {
    const { dateText, selectTimeRadio, page, pageSize, billList } = this.data
    let requestData = {}
    if (selectTimeRadio === 'month') {
      requestData = {
        mouth: dateText,
        page: page,
        limit: pageSize
      }
    } else if (selectTimeRadio === 'day') {
      requestData = {
        day: dateText,
        page: page,
        limit: pageSize
      }
    }
    luckyBeansModel.getLuckyBeansBill(requestData).then((res) => {
      wx.stopPullDownRefresh()
      this.setData({
        luckyBeansInfo: {
          total_income: res.total_income,
          total_outgo: res.total_outgo
        },
        billList: page === 1 ? res.list : billList.concat(res.list),
        totalNum: res.totalcount
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})